package com.slz3.business.module.orderManagement.ordOrder.steward;

import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.module.orderManagement.ordOrder.domain.Order;
import com.slz3.business.module.orderManagement.ordOrder.domain.OrderDetailGroup;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.stream.Collectors.toList;

public class OrdOrderRedisSteward {

    public static List<OrderDetailGroup> findOrderDetailGroupByOrder(Order order){
        String orderDetailGroupUuidKeyList = RK.SERVICE__ORDER__JOIN__ORDER_DETAIL_GROUP__ORDER_UUID__$UUID$.replace("${uuid}",order.getSerOrderUuid());
        Set<Object> orderDetailGroupUuidObjList = Jedis.db0.getSet(orderDetailGroupUuidKeyList);
        List<String> orderDetailGroupInfokeyList = orderDetailGroupUuidObjList.stream().filter(item->item!=null).map(item-> {
            String orderDetailGroupInfokey =  RK.SERVICE__ORDER_DETAIL_GROUP__INFO__ORDER_DETAIL_GROUP_UUID__$UUID$.replace("${uuid}", item.toString());
            return orderDetailGroupInfokey;
        }).collect(toList());

        List<Object> orderDetailGroupInfoObj = Jedis.db0.getListByKey(orderDetailGroupInfokeyList);
        List<OrderDetailGroup> orderDetailGroupList = orderDetailGroupInfoObj.stream().map(item -> {
            Map<String,Object> orderDetailGroupInfoMap = (Map) item;
            OrderDetailGroup orderDetailGroup = OrderDetailGroup.builder().build();
            Steward.map2Obj(orderDetailGroupInfoMap,orderDetailGroup);
            return orderDetailGroup;
        }).collect(toList());
        return orderDetailGroupList;
    }


    public static void closeOrderDetailGroup(OrderDetailGroup orderDetailGroup){
        String orderDetailGroupInfoKey = RK.SERVICE__ORDER_DETAIL_GROUP__INFO__ORDER_DETAIL_GROUP_UUID__$UUID$.replace("${uuid}",orderDetailGroup.getUuid());
        String orderDetailSortWaitPayKey = RK.SERVICE__ORDER_DETAIL_GROUP__SORT__WAIT_PAY__MEMBER_UUID__$UUID$.replace("${uuid}",orderDetailGroup.getSerMemberUuid());

        Map<String,Object> orderDetailGroupMap = Jedis.db0.getMap(orderDetailGroupInfoKey);
        OrderDetailGroup dbOrderDetailGroup = OrderDetailGroup.builder().build();
        Steward.map2Obj(orderDetailGroupMap,dbOrderDetailGroup);

        Jedis.db0.removeFromList(orderDetailSortWaitPayKey,0,orderDetailGroup.getUuid());
        Jedis.db0.set(orderDetailGroupInfoKey,Steward.obj2Map(orderDetailGroup));
    }
}
